//
//  WashCarViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "WashCarViewController.h"

#import "DPDetView.h"
#import "PinJiaCell.h"
#import "WashCell.h"
#import "PingJiaViewController.h"
#import "WashMapViewController.h"
#import "PayForViewController.h"
@interface WashCarViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableDictionary *dic;
    NSMutableArray *datas;
    WashCell *washCell;
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIView *headView;//table头视图
@property (nonatomic,strong)NSMutableDictionary *op;
@property (nonatomic,strong)UILabel *labLine;
@property (nonatomic,strong)UIButton *btn1;
@property (nonatomic,strong)UIButton *btn2;
@property (nonatomic,strong)UIButton *btn3;
//主要是用来接收用户上一次所选的cell的indexpath
@property (nonatomic,strong) NSIndexPath *lastPath;

@property (nonatomic,strong)UILabel *lab1Price;
@property (nonatomic,strong)UILabel *lab2Price;
@property (nonatomic,strong)UILabel *lab3Tit;


@end

@implementation WashCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setBGColor];
    [self showNav:@"店铺详情"];
    dic = [NSMutableDictionary dictionary];
    datas = [NSMutableArray array];
    _op = [NSMutableDictionary dictionary];
    [_op setObject:@"aaa" forKey:@"tag"];
    [self loadUI];
    [self loadData];
    [self loadData2];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64-60) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    DPDetView *view = [[[NSBundle mainBundle] loadNibNamed:@"DPDetView" owner:nil options:nil] lastObject];
    view.frame = CGRectMake(0, 0, ScreenWidth, 260.0);
    _tableView.tableHeaderView = view;
    view.labName.text = _name;
    view.labKm.text = _km;
    view.labCount.text = _count;
    view.labPlace.text = _place;
    view.imageMap.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickMap)];
    [view.imageMap addGestureRecognizer:tap];
    
    //底部视图
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-60, ScreenWidth, 60.0)];
    footView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footView];
    
    UILabel *lines = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    lines.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [footView addSubview:lines];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(ScreenWidth/3*2, 10, ScreenWidth/3, 50.0);
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"去结算" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickJS) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:btn];
    
    _lab1Price = [[UILabel alloc] init];
    _lab1Price.text = @"";
    _lab1Price.textColor = [UIColor redColor];
    _lab1Price.font = [UIFont boldSystemFontOfSize:13.0];
    [footView addSubview:_lab1Price];
    [_lab1Price makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footView.left).offset(8);
        make.top.equalTo(footView.top).offset(18);
    }];
    
    _lab2Price = [[UILabel alloc] init];
    _lab2Price.text = @"";
    _lab2Price.textColor = [UIColor darkGrayColor];
    _lab2Price.font = [UIFont systemFontOfSize:11.0];
    [footView addSubview:_lab2Price];
    [_lab2Price makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_lab1Price.right).offset(8);
        make.bottom.equalTo(_lab1Price.bottom).offset(0);
    }];
    
    _lab3Tit = [[UILabel alloc] init];
    _lab3Tit.text = @"";
    _lab3Tit.textColor = [UIColor darkGrayColor];
    _lab3Tit.font = [UIFont systemFontOfSize:12.0];
    [footView addSubview:_lab3Tit];
    [_lab3Tit makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(footView.left).offset(8);
        make.top.equalTo(_lab1Price.bottom).offset(8);
    }];
    
}
//结算
- (void)clickJS{
    PayForViewController *pay = [[PayForViewController alloc] init];
    [self.navigationController pushViewController:pay animated:YES];
}
//跳转到地图
- (void)clickMap{
    WashMapViewController *map = [[WashMapViewController alloc] init];
    [self.navigationController pushViewController:map animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *cellId = @"Cell1";
        WashCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"WashCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.labName.text = [dic objectForKey:@"name"];
        cell.labDet.text = [dic objectForKey:@"intro"];
        
        NSString *str = [_op objectForKey:@"tag"];
        if ([str isEqualToString:@"aaa"]) {
            
            cell.labPrice1.text = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"limousinediudiuprice"] floatValue]];
            NSString *guideprice = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"limousineprice"] floatValue]];
            NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
            NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:guideprice attributes:attribtDic];
            cell.labPrice2.attributedText = attribtStr;
        }else if ([str isEqualToString:@"sss"]){
            cell.labPrice1.text = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"suvdiudiuprice"] floatValue]];
            NSString *guideprice = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"suvprice"] floatValue]];
            NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
            NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:guideprice attributes:attribtDic];
            cell.labPrice2.attributedText = attribtStr;
        }else{
            cell.labPrice1.text = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"taxidiudiuprice"] floatValue]];
            NSString *guideprice = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"taxiprice"] floatValue]];
            NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
            NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:guideprice attributes:attribtDic];
            cell.labPrice2.attributedText = attribtStr;
        }
        
        NSInteger row = indexPath.row;
        NSInteger oldRow = _lastPath.row;
        if (row == oldRow && _lastPath != nil) {
            cell.images.image = [UIImage imageNamed:@"勾"];
        }else{
            cell.images.image = [UIImage imageNamed:@"椭圆-2-拷贝"];
        }
        
        return cell;
    }else{
        static NSString *cellId = @"Cell2";
        PinJiaCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"PinJiaCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *op = datas[indexPath.row];
        NSString *imageUrl = [NSString stringWithFormat:@"%@%@",urlHead,[op objectForKey:@"avatar"]];
        [cell.images sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"TIM图片20170810135854"]];
        cell.labName.text = [op objectForKey:@"nickname"];
        cell.labDate.text = [op objectForKey:@"commenttime"];
        cell.labText.text = [op objectForKey:@"comment"];
        
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 70.0;
    }else{
        return 100.0;
    }
}
//头视图高度
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        if (_headView) {
            return _headView;
        }
        
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
        _headView.backgroundColor = [UIColor whiteColor];
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 4, 20)];
        lab.backgroundColor = COLOR_RGB(17, 100, 253, 1);
        [_headView addSubview:lab];
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 0.5)];
        line.backgroundColor = [UIColor grayColor];
        [_headView addSubview:line];
        
        UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(lab.frame.origin.x+20, 10, 80, 20)];
        lab2.font = [UIFont systemFontOfSize:15.0];
        lab2.text = @"精选服务";
        [_headView addSubview:lab2];
        
        _btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn1.frame = CGRectMake(ScreenWidth-50, 10, 40, 20);
        _btn1.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_btn1 setTitle:@"的士" forState:UIControlStateNormal];
        [_btn1 setTitleColor:COLOR_RGB(77, 77, 77, 1) forState:UIControlStateNormal];
        [_headView addSubview:_btn1];
        
        _btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn2.frame = CGRectMake(ScreenWidth-130, 10, 70, 20);
        _btn2.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_btn2 setTitle:@"SUV/MVP" forState:UIControlStateNormal];
        [_btn2 setTitleColor:COLOR_RGB(77, 77, 77, 1) forState:UIControlStateNormal];
        [_headView addSubview:_btn2];
        
        _btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn3.frame = CGRectMake(ScreenWidth-200, 10, 60, 20);
        _btn3.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_btn3 setTitle:@"五座轿车" forState:UIControlStateNormal];
        [_btn3 setTitleColor:COLOR_RGB(77, 77, 77, 1) forState:UIControlStateNormal];
        [_headView addSubview:_btn3];
        
        _btn1.tag = 1;
        _btn2.tag = 2;
        _btn3.tag = 3;
        [_btn1 addTarget:self action:@selector(clicks:) forControlEvents:UIControlEventTouchUpInside];
        [_btn2 addTarget:self action:@selector(clicks:) forControlEvents:UIControlEventTouchUpInside];
        [_btn3 addTarget:self action:@selector(clicks:) forControlEvents:UIControlEventTouchUpInside];
        
        _labLine = [[UILabel alloc] initWithFrame:CGRectMake(_btn3.frame.origin.x, 30, 60, 5)];
        _labLine.backgroundColor = [UIColor redColor];
        _labLine.layer.cornerRadius = 10.0;
        [_headView addSubview:_labLine];
        
        return _headView;
    }else{
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
        view.backgroundColor = [UIColor whiteColor];
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 4, 20)];
        lab.backgroundColor = COLOR_RGB(17, 100, 253, 1);
        [view addSubview:lab];
        
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 0.5)];
        line.backgroundColor = [UIColor grayColor];
        [view addSubview:line];
        
        UILabel *lab2 = [[UILabel alloc] initWithFrame:CGRectMake(lab.frame.origin.x+20, 10, 80, 20)];
        lab2.font = [UIFont systemFontOfSize:15.0];
        lab2.text = @"店铺评价";
        [view addSubview:lab2];
        
        UILabel *lab1 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth-200-8, 10, 200, 13)];
        NSString *count = [NSString stringWithFormat:@"总评价：%li",datas.count];
        lab1.text = count;
        lab1.textAlignment = NSTextAlignmentRight;
        lab1.textColor = COLOR_RGB(77, 77, 77, 1);
        lab1.font = [UIFont systemFontOfSize:14.0];
        [view addSubview:lab2];
        
        return view;
    }
    
}

- (void)clicks:(UIButton *)btn{
    if (btn.tag == 1) {
        [UIView animateWithDuration:0.3 animations:^{
            _labLine.frame = CGRectMake(_btn1.frame.origin.x, 30, 40, 5);
        }];
        [_op setObject:@"ddd" forKey:@"tag"];
        [_tableView reloadData];
    }else if (btn.tag == 2){
        [UIView animateWithDuration:0.3 animations:^{
            _labLine.frame = CGRectMake(_btn2.frame.origin.x, 30, 70, 5);
        }];
        [_op setObject:@"sss" forKey:@"tag"];
        [_tableView reloadData];
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            _labLine.frame = CGRectMake(_btn3.frame.origin.x, 30, 60, 5);
        }];
        [_op setObject:@"aaa" forKey:@"tag"];
        [_tableView reloadData];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}
//table尾视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, ScreenWidth, 40.0);
    [btn setTitle:@"查看全部评价" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2+35, 14, 60, 12)];
    NSString *count = [NSString stringWithFormat:@" (%li)",datas.count];
    lab.text = count;
    lab.font = [UIFont systemFontOfSize:12.0];
    lab.textColor = [UIColor redColor];
    [view addSubview:lab];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 40.0;
}

- (void)click{
    PingJiaViewController *pinjia = [[PingJiaViewController alloc] init];
    [self.navigationController pushViewController:pinjia animated:YES];
}
//cell点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        NSInteger newRow = indexPath.row;
        NSInteger oldRow = (self.lastPath != nil)?self.lastPath.row:-1;
        //单选  椭圆-2-拷贝  勾
        if (newRow != oldRow) {
            washCell = [tableView cellForRowAtIndexPath:indexPath];
            washCell.images.image = [UIImage imageNamed:@"勾"];
            
            washCell = [tableView cellForRowAtIndexPath:_lastPath];
            washCell.images.image = [UIImage imageNamed:@"椭圆-2-拷贝"];
            self.lastPath = indexPath;
            
        }
        
        NSString *str = [_op objectForKey:@"tag"];
        if ([str isEqualToString:@"aaa"]) {
            _lab1Price.text = [NSString stringWithFormat:@"%.2f",[[dic objectForKey:@"limousinediudiuprice"] floatValue]];
            NSString *guideprice = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"limousineprice"] floatValue]];
            NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
            NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:guideprice attributes:attribtDic];
            
            _lab2Price.attributedText = attribtStr;
            _lab3Tit.text = [dic objectForKey:@"name"];
        }else if ([str isEqualToString:@"sss"]){
            _lab1Price.text = [NSString stringWithFormat:@"%.2f",[[dic objectForKey:@"suvdiudiuprice"] floatValue]];
            NSString *guideprice = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"suvprice"] floatValue]];
            NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
            NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:guideprice attributes:attribtDic];
            
            _lab2Price.attributedText = attribtStr;
            _lab3Tit.text = [dic objectForKey:@"name"];
        }else{
            _lab1Price.text = [NSString stringWithFormat:@"%.2f",[[dic objectForKey:@"taxidiudiuprice"] floatValue]];
            NSString *guideprice = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"taxiprice"] floatValue]];
            NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
            NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:guideprice attributes:attribtDic];
            
            _lab2Price.attributedText = attribtStr;
            _lab3Tit.text = [dic objectForKey:@"name"];
        }
    }
    
}

//洗车店铺详情
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_lng forKey:@"longitude"];
    [parmas setObject:_lat forKey:@"latitude"];
    [parmas setObject:_carId forKey:@"id"];
    NSLog(@"parmas:%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/CarWash/details/id/7",urlHead];
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"洗车res:%@",responseObject);
        dic = [[responseObject objectForKey:@"result"] objectForKey:@"details"];
        
        
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}
//洗车评论
- (void)loadData2{
    NSString *url = [NSString stringWithFormat:@"%@/App/Merchantcomment/carwash/merchantid/25/p/1",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"洗车评论res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            datas = [[responseObject objectForKey:@"result"] objectForKey:@"comment"];
        }
        
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
